//
//  GLBaseHTTPTool.m
//  SXBPurchase
//
//  Created by huanggulong on 2017/10/8.
//  Copyright © 2017年 历山大亚. All rights reserved.
//

#import "GLBaseHTTPTool.h"
#import <MJExtension/MJExtension.h>
#import "GLHTTPTool.h"

@implementation GLBaseHTTPTool

+ (NSURLSessionDataTask *)baseRequestURL:(NSString *)url requestParam:(GLHTTPParam *)param resultModelType:(Class)modelClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    if (![url hasPrefix:@"http://"] && ![url hasPrefix:@"https://"]){
        url = [NSString stringWithFormat:@"%@/%@",Domain,url];
    }
    return [GLHTTPTool postWithURL:url params:param.gl_keyValue success:^(id json) {
        GLHTTPResult *user = [modelClass mj_objectWithKeyValues:json];
        if (success) {
            success(user);
        }
    } failure:failure];
}

+ (NSURLSessionDataTask *)jsonRequestURL:(NSString *)url requestParam:(GLHTTPParam *)param resultModelType:(Class)modelClass success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    if (![url hasPrefix:@"http://"] && ![url hasPrefix:@"https://"]){
        url = [NSString stringWithFormat:@"%@/%@",Domain,url];
    }
    return [GLHTTPTool jsonWithURL:url params:param.gl_keyValue success:^(id json) {
        GLHTTPResult *user = [modelClass mj_objectWithKeyValues:json];
        if (success) {
            success(user);
        }
    } failure:failure];
}

+(NSURLSessionDataTask *)getRequestURL:(NSString *)url requestParam:(GLHTTPParam *)param resultModelType:(Class)modelClass success:(void (^)(id result))success failure:(void (^)(NSError *error))failure{
    if (![url hasPrefix:@"http://"] && ![url hasPrefix:@"https://"]){
        url = [NSString stringWithFormat:@"%@/%@",Domain,url];
    }
    return [GLHTTPTool getWithURL:url params:param.gl_keyValue success:^(id json) {
        GLHTTPResult *user = [modelClass mj_objectWithKeyValues:json];
        if (success) {
            success(user);
        }
    } failure:failure];
}

+(NSURLSessionDataTask *)baseRequestURL:(NSString *)url requestParam:(GLHTTPParam *)param success:(void (^)(GLHTTPResult *))success failure:(void (^)(NSError *))failure{
    if (![url hasPrefix:@"http://"] && ![url hasPrefix:@"https://"]){
        url = [NSString stringWithFormat:@"%@/%@",Domain,url];
    }
    return [GLHTTPTool postWithURL:url params:param.gl_keyValue success:^(id json) {
        GLHTTPResult *user = [GLHTTPResult mj_objectWithKeyValues:json];
        if (success) {
            success(user);
        }
    } failure:failure];
}

+(NSURLSessionDataTask *)jsonRequestURL:(NSString *)url requestParam:(GLHTTPParam *)param success:(void (^)(GLHTTPResult * result))success failure:(void (^)(NSError *error))failure{
    if (![url hasPrefix:@"http://"] && ![url hasPrefix:@"https://"]){
        url = [NSString stringWithFormat:@"%@/%@",Domain,url];
    }
    return [GLHTTPTool jsonWithURL:url params:param.gl_keyValue success:^(id json) {
        GLHTTPResult *user = [GLHTTPResult mj_objectWithKeyValues:json];
        if (success) {
            success(user);
        }
    } failure:failure];
}

+(NSURLSessionDataTask *)getRequestURL:(NSString *)url requestParam:(GLHTTPParam *)param success:(void (^)(GLHTTPResult *))success failure:(void (^)(NSError *))failure{
    if (![url hasPrefix:@"http://"] && ![url hasPrefix:@"https://"]){
        url = [NSString stringWithFormat:@"%@/%@",Domain,url];
    }
    return [GLHTTPTool getWithURL:url params:param.gl_keyValue success:^(id json) {
        GLHTTPResult *user = [GLHTTPResult mj_objectWithKeyValues:json];
        if (success) {
            success(user);
        }
    } failure:failure];
}

+(void)setLoginToken:(NSString *)loginToken{
    [GLHTTPTool setLoginToken:loginToken];
}

@end
